package com.lizhi.updata;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lizhi.model.BookModel;
import com.lizhi.util.Util;

public class ChangeBookProperty {
	private String host = "http://www.sbkk8.cn";
	private String url = "https://freebook.firebaseio.com/books.json";

	public ChangeBookProperty() {

		try {
			List<BookModel> bookModels = new ArrayList<BookModel>();
			CloseableHttpClient httpClient = HttpClients.createDefault();
			HttpGet httpPost = new HttpGet(url);
			CloseableHttpResponse response = httpClient.execute(httpPost);
			InputStream inputStream = response.getEntity().getContent();
			JSONObject jsonObject = JSON.parseObject(Util.readStream(inputStream));

			Set<String> keySet = jsonObject.keySet();
			for (String string : keySet) {
				BookModel bookModel = JSON.toJavaObject(jsonObject.getJSONObject(string), BookModel.class);
				bookModels.add(bookModel);
				System.out.println(bookModel.name);
			}

			System.out.println("pageNUM:" + bookModels.size());

			// Map<String, String> map = Util.readBookBusiness("book_business.txt");

			 Util.clearData("https://freebook.firebaseio.com/books.json");

			for (int i = 0; i < bookModels.size(); i++) {
				BookModel bookModel = bookModels.get(i);
				String[] temps = bookModel.img.split(host);
				if(temps.length>2){
					System.out.println(bookModel.img);
					bookModel.img=host+temps[2];
					System.out.println(bookModel.img);
				}
				 new UploadDataTask("https://freebook.firebaseio.com/books.json", JSON.toJSONString(bookModel)).start();
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {

		new ChangeBookProperty();

	}

}
